package elephant.servers.gameserver.service;

import elephant.servers.gameserver.GameServer;
import elephant.servers.gameserver.codec.ResponseMessage;
import elephant.servers.gameserver.core.GameRequest;
import elephant.servers.gameserver.session.GameSession;

/**
 * 
 * @author icecooly
 *
 */
public class ServiceContextImpl implements ServiceContext{
	//
	private GameRequest gameRequest;
	private GameServer gameServer;
	//
	private ResponseMessage rsp;
	//
	public ServiceContextImpl(GameServer gameServer) {
		rsp=new ResponseMessage();
		this.gameServer=gameServer;
	}
	//--------------------------------------------------------------------------
	public GameServer getGameServer() {
		return gameServer;
	}
	
	public void rsp(Object body){
		rsp.body=body;
	}
	
	public GameSession getSession(){
		return gameRequest.session;
	}

	public String getServiceId(){
		return gameRequest.serviceId;
	}
	
	//--------------------------------------------------------------------------
	/**
	 * @return the gameRequest
	 */
	public GameRequest getGameRequest() {
		return gameRequest;
	}
	/**
	 * @param gameRequest the gameRequest to set
	 */
	public void setGameRequest(GameRequest gameRequest) {
		this.gameRequest = gameRequest;
		rsp.requestId=gameRequest.requestId;
		rsp.serviceId=gameRequest.serviceId;
		rsp.errCode=ResponseMessage.ERR_CODE_OK;
		rsp.bodySerializeType=gameRequest.bodySerializeType;
	}
	//
	public ResponseMessage getResponseMessage(){
		return rsp;
	}
	//
	public void sendError(int errCode,String msg){
		rsp.errCode=errCode;
		rsp.errMsg=msg;
	}
	//
	public void flush() {
		getSession().writeResponseMessage(rsp);
	}
	//
}
